<?php
include(ROOT_PATH . '/lib/request.lib.php');
class scupView {
	var $template_dir = '';
	var $cache_dir = '';
	var $compile_dir = '';

	var $template_filename = '';

	var $widget_dir = '';

	var $widget_base_load = null;
	var $active_base_load = null;	
	var $phpscup_div = null;
	
	var $cache_lifetime = 3600; // 缓存更新时间, 默认 3600 秒
	var $direct_output = false; // 是否直接输出
	var $caching = true; // 是否缓存
	var $template = array ();
	var $force_compile = false; // 是否需要强制编译
	var $gzip = false;

	var $_var = array ();
	
	var $_logic_app = array ();
	
	var $_current_file = '';

	var $_nowtime = null;
    var $view_start_or_stop = false ;
    var $router = null ; 
    var $filterworlds = null ; 	
	function __construct() {
		$this->scupView();
	}

	function scupView() {
		$this->_nowtime = time();
		$this->router = include(ROOT_PATH.'/data/router/router.php');
	
	}

	/**
	 * 变量赋值
	 */
	function assign($tpl_var, $value = '') {
		if ($tpl_var != '') {
			$this->_var[$tpl_var] = $value;
		}
	}

	/**
	 变量引用赋值
	 */
	function _assign($tpl_var, & $value) {
		if ($tpl_var != '') {
			$this->_var[$tpl_var] = & $value;
		}
	}

	//从新得到get数据，用于静态网页用2表示，和伪静态网页用1表示
	function _re_get_url() 
	{
	
		if (SYSTEM_URL_REWRITE == 1) 
		{   
		    Request::Instance();
			$script_name = basename($_SERVER['SCRIPT_NAME']);
			$request_str = strstr($_SERVER['REQUEST_URI'], '?');
			$request_str = substr($request_str, 1);
			$request_str = preg_replace("/\.(html|htm|php)$/is","",$request_str);
 
			$request_arr = explode('-', $request_str);

			for ($i = 1; $i < count($request_arr);) {
				if (empty ($request_arr[$i])) {
					$i++;
					continue;
				}
                if(!isset($request_arr[$i +1]))
				{
				$request_arr[$i +1]='';
				}	
				$_GET[trim($request_arr[$i])] = trim($request_arr[$i +1]);
				$i = $i +2;
			}
		} else if (SYSTEM_URL_REWRITE == 2) {
		
		}
		/*
		 get的sx数据格式是sx=sxa.10sxb.22
		*/
		if(isset($_GET['sx'])&&!empty($_GET['sx']))
		{
		      $app = !empty ($_GET['app']) ? trim($_GET['app']) : $GLOBALS['SCup']->SCup_app_name;		
		      $module_list_data = & $GLOBALS['SCup']->_get_module_list_data();			  
		      if(is_array($module_list_data)&& isset($module_list_data[$app]['selectlist'])&&!empty($module_list_data[$app]['selectlist']))
			  {  
                  $selectdata = unserialize($module_list_data[$app]['selectlist']);	
				  $sxtempdata = del_empty_var(explode('sx', $_GET['sx']));
				  $sxdata = array();
				  $selectstr = '';
				  foreach($sxtempdata as $key=>$value)
				  {
				    $temp = explode(".",$value);
					$sxdata[$temp[0]] = $temp[1];
					if($temp[1]==0)
					{					
					   unset($_GET['sx']);
					   return;
					}
				  }
				  foreach($selectdata as $key=>$value)
				  {
                      if(isset($sxdata[$value['to']])&&isset($value['filedname'])&&!empty($value['filedname']))
					  {
					        if(isset($value['data']))
							{							
							     $selectstr.= ' '.str_replace(array(';suang;',";dang;"),array('"',"'"),$value['data'][$sxdata[$value['to']]]['where']).' and'; 
							}
							else
							{
							     $selectstr.= ' '.$value['filedname']."='".$sxdata[$value['to']]."' and";							
							}					  
					  }
				  }	
                  if (!defined("SX_SELECT_STR")) 
				  {
	                  define("SX_SELECT_STR", substr($selectstr, 0,-3));
                  }	
			  }
		}
		
	}

	//产生url链接，用于静态网页，和伪静态网页
	function _write_url($url_str) {
		if (preg_match("/^http:\/\//i", $url_str)) {
			return $url_str;
		}
		
		$url_array = explode('?', $url_str);
		$url_num = count($url_array);

		if ($url_num >= 3) {
			return SITE_URL;
		}

		if (!isset ($url_array[1])) {
			$url_array[1] = '';
		}
		$temparr = explode('&', $url_array[1]);
		$temp_router = array();
		foreach ($temparr as $value) 
		{
			if ($value) 
			{
				$value = explode('=', $value);
				$temp_router[$value[0]] = $value[1];
			}
		}
        $routerkey = '';
        $routerid = '';        		
        if( isset($temp_router['act']) && $temp_router['act']=='show')
        {
            foreach($this->router as $key => $value) {
			    if($value['app'] == $temp_router['app']) {
				   $routerkey = $key;
				   $routerid = $temp_router['id'];
				   break;
				}
			}
        }		
         if (SYSTEM_URL_REWRITE == 1 && $routerkey  ) {
		    $scupname = '';
		    if($GLOBALS['SCup']->SCup_name!='scup') {
			    $scupname = '/'.$GLOBALS['SCup']->SCup_name;
			}
		    if(!empty($routerid)) {
			    return SITE_URL .$scupname. '/'.$routerkey.'/'.$routerid.'.html';
			}else{
			    return SITE_URL .$scupname. '/'.$routerkey.'html';
			}
		} 
		
		if (SYSTEM_URL_REWRITE == 1) {

			if ($url_array[0] == 'index.php' || $url_array[0] == '') {
				$url_array[0] = '?';
			}

			if ($url_array[0] == '?') {
				$url = SITE_URL . '/?';
			} else {
				$url = SITE_URL . '/' . $url_array[0] . '?';
			}
		   $sxdata = array();
		   if(isset($_GET['sx'])&&!empty($_GET['sx']))
		   {
				  $sxtempdata = del_empty_var(explode('sx', $_GET['sx']));
				  foreach($sxtempdata as $key=>$value)
				  {
				        $temp = explode(".",$value);
						$sxdata[$temp[0]] = $temp[1];					         

				  }
		    }
			$arr = explode('&', $url_array[1]);
			foreach ($arr as $value) 
			{
				if ($value) 
				{
					$value = explode('=', $value);
					if($value[0]=='sx')
					{
				        $sxtempdata = del_empty_var(explode('sx', $value[1]));
				        foreach($sxtempdata as $key=>$value)
				        {
				            $temp = explode(".",$value);
						    $sxdata[$temp[0]] = $temp[1];						         							
				        }					   
					}
					else
					{
					$url .= '-' . $value[0] . '-' . $value[1];					
					}

				}
			}
            if(!empty($sxdata))
			{   $str = '';
			    foreach($sxdata as $key => $value)
				{  if($value!='0')
				   {
				   $str .= 'sx'.$key.'.'.$value;
				   }
				}
			    $url .= '-sx-'.$str;
			}
			
			$url .= '.html'; //url链接后缀如.html,.php或为空格""			 
		} else if (SYSTEM_URL_REWRITE == 2) {
	
			if ($url_array[0] == 'index.php' || $url_array[0] == '') {
				$url_array[0] = '?';
			}
		    $scupname = '';
		    if($GLOBALS['SCup']->SCup_name!='scup') {
			    $scupname = '/'.$GLOBALS['SCup']->SCup_name;
			}				
			if ($url_array[0] == '?') {
				$url = SITE_URL .$scupname . '/?';
			} else {
				$url = SITE_URL .$scupname . '/' . $url_array[0] . '?';
			}		
 		   $sxdata = array();
		   if(isset($_GET['sx'])&&!empty($_GET['sx']))
		   {
				  $sxtempdata = del_empty_var(explode('sx', $_GET['sx']));
				  foreach($sxtempdata as $key=>$value)
				  {
				        $temp = explode(".",$value);
						$sxdata[$temp[0]] = $temp[1];					         

				  }
		    }
			$arr = explode('&', $url_array[1]);
			foreach ($arr as $value) 
			{
				if ($value) 
				{
					$value = explode('=', $value);
					if($value[0]=='sx')
					{
				        $sxtempdata = del_empty_var(explode('sx', $value[1]));
				        foreach($sxtempdata as $key=>$value)
				        {
				            $temp = explode(".",$value);
						    $sxdata[$temp[0]] = $temp[1];						         							
				        }					   
					}
					else
					{
					$url .= '&' . $value[0] . '=' . $value[1];					
					}

				}
			}
            if(!empty($sxdata))
			{   $str = '';
			    foreach($sxdata as $key => $value)
				{  if($value!='0')
				   {
				   $str .= 'sx'.$key.'.'.$value;
				   }
				}
			    $url .= '&sx='.$str;
			}
           
			
		} else 
		{
		    $scupname = '';
		    if($GLOBALS['SCup']->SCup_name!='scup') {
			    $scupname = '/'.$GLOBALS['SCup']->SCup_name;
			}		
            $url = SITE_URL .$scupname .'/index.php?' . $url_array[1];		

		}
		return $url;
	}

	
    //产生url链接，用于静态网页，和伪静态网页
    function _write_url_html($url_str)
    {     
	
		$url_array=explode('&',$url_str);
		$url_num = count($url_array);
		
		if($url_num >=3)
		{
		   return  SITE_URL;	
		}

		if(!isset($url_array[0])||empty($url_array[0]))
		{
		   return  SITE_URL; 
		}
		

		if(!isset($url_array[1])||empty($url_array[1]))
		{
		   return  SITE_URL; 
		}
		
		$cookie_lang=get_cookie('languages');
	    if(empty($cookie_lang))
		{	
		   $lang_type = LANG;		  	  
		}
		else
		{
		 $lang_type = $cookie_lang;
		} 


	    $url = SITE_URL.'/'.$url_array[0].'/'. $url_array[1] .'/'.$lang_type. '.html';			 
  	
		return $url;
    }	

	
     function html($id,$siteurl,$app)
    {

		$content = @file_get_contents($siteurl);
		
		
		$cookie_lang=get_cookie('languages');
	    if(empty($cookie_lang))
		{	
		   $lang_type = LANG;		  	  
		}
		else
		{
		 $lang_type = $cookie_lang;
		} 

		$cachefilename = ROOT_PATH .'/'.$app.'/'. $id .'/'.$lang_type. '.html'; 


        get_dir(ROOT_PATH .'/'.$app.'/'. $id);
				 
		 file_put_contents($cachefilename,$content);   

    }	

        function delhtml($id,$app)
    {


 
		del_file(ROOT_PATH .'/'.$app.'/'. $id .'/');
 

    }	

    //产生url链接，用于静态网页，和伪静态网页
    function _write_url_modhtml($url_str)
    {     
	
		$url_array=explode('&',$url_str);
		$url_num = count($url_array);
		
		if($url_num >=3)
		{
		   return  SITE_URL;	
		}

		if(!isset($url_array[0])||empty($url_array[0]))
		{
		   return  SITE_URL; 
		}
        if(isset($url_array[1])&&empty($url_array[1]))$url_array[1]='';
	    $url = SITE_URL.'/content/'.$url_array[0]. $url_array[1] .'.html';			 
  	
		return $url;
    }	

     function modhtml($id,$siteurl,$app)
    {

		$content = @file_get_contents($siteurl);
		
/*		
		$cookie_lang=get_cookie('languages');
	    if(empty($cookie_lang))
		{	
		   $lang_type = LANG;		  	  
		}
		else
		{
		 $lang_type = $cookie_lang;
		} 
  */      
	    if(empty($id))$id='';
		$cachefilename = ROOT_PATH .'/content/'. $app.$id .'.html'; 


        get_dir(ROOT_PATH .'/content');
				 
		 file_put_contents($cachefilename,$content);   

    }	

        function delmodhtml($id,$app)
       { 
			if(empty($id))$id='';
		del_file(ROOT_PATH .'/content/'. $app.$id .'.html'); 
       }


	/**
	* 显示页面函数
	*
	* @access  public
	* @param   string      $filename
	* @param   sting      $cache_id
	*
	* @return  void
	*/
	function display($filename, $cache_id = '') {
		$icon_cache = false;
        $this->template_filename = $filename;
		
		$cookie_lang = get_cookie('languages');
		if (empty ($cookie_lang)) {
			$lang_type = LANG;
		} else {
			$lang_type = $cookie_lang;
		}
		$cachefilename = $this->cache_dir . '/' . $lang_type . '/' . $filename . $cache_id . '.html';
		$cache_time = @ filemtime($cachefilename);

		if ($cache_id && $this->caching) {
			if (is_file($cachefilename) && ($this->_nowtime - $this->cache_lifetime <= $cache_time)) {
				echo @ file_get_contents($cachefilename);
				exit ();

			} else {
				$icon_cache = true;
			}
		}

		$out = $this->_template($filename);

		ob_start();
		eval ('?' . '>' . trim($out));
		$out = ob_get_contents();
		ob_end_clean();

		if ($icon_cache && ($this->_nowtime - $this->cache_lifetime > $cache_time)) {
			if (!file_exists($cachefilename)) {
				get_dir($this->cache_dir . '/' . $lang_type);
			}
			file_put_contents($cachefilename, $out);
		}
		echo $out;
	}

	/**
	 * 处理模板文件
	 */
	function _template($filename) {
		$vfilename = $this->template_dir . '/' . $filename;

		$cfilename = $this->compile_dir . '/' . $filename . '.phpscup';
		
		$sfilename = ROOT_PATH .'/'.$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE. '/tempview/' . $filename;

		if (file_exists($cfilename) === false  || (@ filemtime($vfilename) > @ filemtime($cfilename)) || (@ filemtime($sfilename) > @ filemtime($cfilename))) 
		{
		    $tempfilename = '';				
		    if(is_file($sfilename) && @ filemtime($sfilename) > @ filemtime($vfilename))
			{			
		       $tempfilename = $sfilename;				   			
			}
			else
			{
			   if (!is_file($vfilename)) 
			   {
				exit ("no exist view");
			   }
		       $tempfilename = $vfilename;				
		
			}  
             

			$out = "";
			if (!($out = file_get_contents($tempfilename))) {
				return false;
			}
			$out = $this->template_str($out);

			if (!file_exists($cfilename)) {
				get_dir($this->compile_dir. '/' . $filename);

			}
            if($this->view_start_or_stop==true)
			{
			   if (!file_put_contents($cfilename, $out)) {
				return false;
			   }				
			}else
			{
			   if (!file_put_contents($cfilename, '<?php if(!defined("IN_PHPSCUP"))exit("hacking");?>' . $out)) {
				return false;
			   }			
			}

		} else {

			if (!($out = file_get_contents($cfilename))) {
				return false;
			}
		}
		return $out; // 返回html数据
	}

	/**
	 * 处理字符串函数
      单片文章静态标签写法: <!--{urlhtml ('new&id')}-->，new表示那个app,id是指某篇文章的id号，中间用"&"隔开,他们顺序不能反，app要在前。
	  模块分类页静态标签写法:<!--{urlmodhtml ('new&catid')}-->new表示那个app,catid是指某个分类的id号，中间用"&"隔开，如果不写"&catid"则调用的是模块主页，如果写了则调用某分类页,他们顺序不能反，app要在前。。
      韦静态标签写法: <!--{url ('?app=new&act=index&cat=$_value.cat_id')}-->
	  
	  <phpscup:power (scup-leaveword:0-index||job-index&&order-index&&aa-index||pic-index) />
	  <phpscup:endpower />
	  这两个标签是一对，用于判断是否有scup-leaveword:0-index||job-index&&order-index&&aa-index||pic-index这些操作权限的与或运算,如果结果为1则可操作里面的内容，如果是0则不能
	  
	 */
	function template_str($fileContent) {
		$fileContent = preg_replace('/^(\xef\xbb\xbf)/', '', $fileContent); //EFBBBF    
		
		$fileContent = preg_replace("/\<phpscup\:icon\s*(.+?)\s*\/\>/ies", "\$this->_phpscup_to_icon('\\1')", $fileContent);
		
		$fileContent = preg_replace("/\<phpscup\:sx\s*(.+?)\s*\>/ies", "\$this->_phpscup_to_sx('\\1')", $fileContent);	
		
		$fileContent = preg_replace("/\<\/phpscup\:sx\s*\>/is", "<?php } ?>", $fileContent);
		
		$fileContent = preg_replace("/\<phpscup\:form\s*(.+?)\s*\/\>/ies", "\$this->_phpscup_to_html('\\1')", $fileContent);
		
		$fileContent = preg_replace("/\\\$([0-9a-zA-Z.|:_\"',$;]*)/ies", "\$this->var_replace('\\1')", $fileContent);

//		$fileContent = preg_replace("/#([0-9a-zA-Z.|:_\"',#$;]*)/ies", "\$this->var_replace('\\1')", $fileContent);		

		$fileContent = preg_replace("/\<\!\-\-\s*\{url\s*\(\s*(.+?)\s*\)\s*\}\s*\-\-\>/ies", "\$this->_url('\\1')", $fileContent);

        $fileContent = preg_replace("/\<\!\-\-\s*\{urlhtml\s*\(\s*(.+?)\s*\)\s*\}\s*\-\-\>/ies", "\$this->_urlhtml('\\1')", $fileContent);

		$fileContent = preg_replace("/\<\!\-\-\s*\{css\s*\(\s*(.+?)\s*\)\s*\}\s*\-\-\>/ies", "\$this->_css('\\1')", $fileContent);

		$fileContent = preg_replace("/\<\!\-\-\s*\{js\s*\(\s*(.+?)\s*\)\s*\}\s*\-\-\>/ies", "\$this->_js('\\1')", $fileContent);
		
		$fileContent = preg_replace("/\<\!\-\-\s*\{if\s*\(\s*(.+?)\s*\)\s*\}\s*\-\-\>/ies", "\$this->__replace('<?php if(\\1) { ?>')", $fileContent);

		$fileContent = preg_replace("/\<\!\-\-\s*\{else\s*if\s+\(\s*(.+?)\s*\)\s*\}\s*\-\-\>/ies", "\$this->__replace('<?php } else if(\\1) { ?>')", $fileContent);		
		$fileContent = preg_replace("/\<\!\-\-\s*\{else\}\s*\-\-\>/is", "<?php } else { ?>", $fileContent);

		$fileContent = preg_replace("/\<\!\-\-\s*\{\/if\}\s*\-\-\>/is", "<?php } ?>", $fileContent);
		
		$fileContent = preg_replace("/\<\!\-\-\s*\{foreach\s*\(\s*(.+?)\s*\)\s*\}\s*\-\-\>/ies", "\$this->__replace('<?php foreach((array)\\1) { ?>')", $fileContent);		
		$fileContent = preg_replace("/\<\!\-\-\s*\{\/foreach\}\s*\-\-\>/is", "<?php } ?>", $fileContent);		

		$fileContent = preg_replace("/\<\!\-\-\s*\{inc\s*\(\s*(.+?)\s*\)\s*\}\s*\-\-\>/ies", "\$this->_include(\"\\1\")", $fileContent);

		$fileContent = preg_replace("/\<phpscup\:foreach\s*\(\s*(.+?)\s*\)\s*\/\>/ies", "\$this->__replace('<?php foreach((array)\\1) { ?>')", $fileContent);		
		$fileContent = preg_replace("/\<phpscup\:endforeach\s*\/\>/is", "<?php } ?>", $fileContent);

		$fileContent = preg_replace("/\<phpscup\:if\s*\(\s*(.+?)\s*\)\s*\/\>/ies", "\$this->__replace('<?php if(\\1) { ?>')", $fileContent);
		$fileContent = preg_replace("/\<phpscup\:power\s*\(\s*(.+?)\s*\)\s*\/\>/ies", "\$this->__replace('<?php if(icon_power_limit(\"\\1\")) { ?>')", $fileContent);
		
		$fileContent = preg_replace("/\<phpscup\:elseif\s+\(\s*(.+?)\s*\)\s*\/\>/ies", "\$this->__replace('<?php } else if(\\1) { ?>')", $fileContent);
		$fileContent = preg_replace("/\<phpscup\:else\s*\/\>/is", "<?php } else { ?>", $fileContent);

		$fileContent = preg_replace("/\<phpscup\:endpower\s*\/\>/is", "<?php } ?>", $fileContent);
		$fileContent = preg_replace("/\<phpscup\:endif\s*\/\>/is", "<?php } ?>", $fileContent);
		

		
		$fileContent = preg_replace("/\<phpscup\:inc\s*\(\s*(.+?)\s*\)\s*\/\>/ies", "\$this->_include(\"\\1\")", $fileContent);

		$fileContent = preg_replace("/\<phpscup\:widget\s*\(\s*(.+?)\s*\)\s*\/\>/ies", "\$this->_widget('\\1')", $fileContent);

		$fileContent = preg_replace("/\<phpscup\:url\s*\(\s*(.+?)\s*\)\s*\/\>/ies", "\$this->_url('\\1')", $fileContent);

        $fileContent = preg_replace("/\<phpscup\:urlhtml\s*\(\s*(.+?)\s*\)\s*\/\>/ies", "\$this->_urlhtml('\\1')", $fileContent);

        $fileContent = preg_replace("/\<phpscup\:urlmodhtml\s*\(\s*(.+?)\s*\)\s*\/\>/ies", "\$this->_urlmodhtml('\\1')", $fileContent);

		$fileContent = preg_replace("/\<phpscup\:css\s*\(\s*(.+?)\s*\)\s*\/\>/ies", "\$this->_css('\\1')", $fileContent);

		$fileContent = preg_replace("/\<phpscup\:js\s*\(\s*(.+?)\s*\)\s*\/\>/ies", "\$this->_js('\\1')", $fileContent);


		$fileContent = preg_replace("/\{([\\\$a-zA-Z0-9_\[\]\\\ \-\'\,\;\%\(\)\*\/\.\>\'\"\$\x7f-\xff]+)\}/ies", "\$this->__replace('<?php echo \\1;?>')", $fileContent);


		return $fileContent;
	}


	function _widget($string) {
		$string = preg_replace("/\{([\\\$a-zA-Z0-9_\[\]\\\ \-\'\,\&\;\%\*\/\.\(\)\>\'\"\$\x7f-\xff]+)\}/ies", "\$this->__replace('\\1')", $string);
		$string = str_replace("'", "", $string);

		unset ($array, $out, $wfilename);
		$array = explode('&', $string);
		$array[0] = trim($array[0]);
		$array[1] = trim($array[1]);

		$wfilename = ROOT_PATH . '/scup/widget/' . $array[0] . '/widget.html';
		$out = file_get_contents($wfilename);
		$out = $this->template_str($out);

		return '<?php $this->__widget(\'' . $array[0] . "','" . $array[1] . '\'); ?>' . "\n" . $out;
	}

	function __widget($app, $act) {
		if ($this->widget_base_load == null) {

			require_once (ROOT_PATH . '/scupcore/widget.base.php');
			$this->widget_base_load = '1';
		}
		require ($this->widget_dir . '/' . $app . '/widget.php');

		$class_name = ucfirst($app) . 'Widget';
		new $class_name ($act);
	}

	function __active($app, $active) {
			
		if ($this->active_base_load == NULL) {

			require_once (ROOT_PATH . '/scupcore/active.base.php');
			$this->active_base_load = '1';
		}	
		require_once (ROOT_PATH .'/scup/view/'.VIEW_TYPE. '/active/'.$active.'/'.$app.'.active.'.$active.'.php');
		$class_name = ucfirst($app) . 'Active';
		$appbody = new $class_name ($app);
		return $appbody->_return;		
	}

	function _css($string) {
		$string1 = preg_replace("/\{([\\\$a-zA-Z0-9_\[\]\\\ \-\'\,\&\;\%\*\/\.\(\)\>\'\"\$\x7f-\xff]+)\}/ies", "\$this->__replace('\\1')", $string);
		if ($string1 == $string) {
			$string1 = str_replace("'", "", $string1);

			$string1 = '<?php echo SITE_URL;?>/<?php echo $GLOBALS[\'SCup\']->SCup_name ;?>/view/<?php echo  VIEW_TYPE ;?>/style/<?php echo CSS_TYPE ;?>/css/' . $string1;

			return '<link href="' . $string1 . '" rel="stylesheet" type="text/css">';
		} else {
			$string1 = str_replace("'", "", $string1);
			$string1 = '<?php echo $GLOBALS[\'SCup\']->SCup_name ;?>/view/<?php echo  VIEW_TYPE ;?>/style/<?php echo CSS_TYPE ;?>/css/' . $string1;

			return '<link href="' . $string1 . '" rel="stylesheet" type="text/css">';
		}
	}

    function _phpscup_to_html($str = '')
	{
	    if(empty($str))
		{
		 return '';
		}
		if ($this->phpscup_div == null) 
		{ 
			require_once (ROOT_PATH . '/lib/div.lib.php');
			$this->phpscup_div = new PhpSCup_div();
		}
		
	   return $this->phpscup_div->phpdiv_to_htmldiv($str);
	   	
	}

	function _phpscup_to_icon($str = '')
	{
	    if(empty($str))
		{
		 return '';
		}
		if ($this->phpscup_div == null) 
		{
			require_once (ROOT_PATH . '/lib/div.lib.php');
			$this->phpscup_div = new PhpSCup_div();
		}
		
	   return $this->phpscup_div->phpdiv_to_htmlicon($str);
	   	
	}
	
	function _phpscup_to_sx($str = '')
	{
	    if(empty($str))
		{
		 return '';
		}
		if ($this->phpscup_div == null) 
		{
			require_once (ROOT_PATH . '/lib/div.lib.php');
			$this->phpscup_div = new PhpSCup_div();
		}
		
	   return $this->phpscup_div->phpdiv_to_sx($str);
	   	
	}	
	
	function _js($string) {
		$string1 = preg_replace("/\{([\\\$a-zA-Z0-9_\[\]\\\ \-\'\,\&\;\%\*\/\.\(\)\>\'\"\$\x7f-\xff]+)\}/ies", "\$this->__replace('\\1')", $string);
		if ($string1 == $string) {
			$string1 = str_replace("'", "", $string1);
			$string1 = '<?php echo SITE_URL;?>/<?php echo $GLOBALS[\'SCup\']->SCup_name ;?>/view/<?php echo  VIEW_TYPE ;?>/style/<?php echo CSS_TYPE ;?>/js/' . $string1;
			return '<script type="text/javascript" src="' . $string1 . '"></script>';
		} else {
			$string1 = str_replace("'", "", $string1);
			$string1 = '<?php echo $GLOBALS[\'SCup\']->SCup_name ;?>/view/<?php echo  VIEW_TYPE ;?>/style/<?php echo CSS_TYPE ;?>/js/' . $string1;
			return '<script type="text/javascript" src="' . $string1 . '"></script>';
		}
	}

	function _url($string) {
		$string1 = preg_replace("/\{([\\\$a-zA-Z0-9_\[\]\\\ \-\'\,\;\%\*\/\.\(\)\>\'\"\$\x7f-\xff]+)\}/ies", "\$this->__replace('\\1')", $string);
		if ($string1 == $string) {
			$string1 = str_replace("'", "", $string1);
		    $string1 = preg_replace("/\\\$([\\\$a-zA-Z0-9_\[\]\\\ \-\'\,\;\%\(\)\*\/\.\>\'\"\$\x7f-\xff]+)/ies", "\$this->var_add_str('\\1')", $string1);
			return '<?php echo $this->_write_url("' . $string1 . '"); ?>';
		} else {
			$string1 = str_replace("'", "", $string1);
		    $string1 = preg_replace("/\\\$([\\\$a-zA-Z0-9_\[\]\\\ \-\'\,\;\%\(\)\*\/\.\>\'\"\$\x7f-\xff]+)/ies", "\$this->var_add_str('\\1')", $string1);
			return '<?php echo $this->_write_url("' . $string1 . '"); ?>';
		}
	}

    function _urlhtml($string)
	{	   
		$string1 = preg_replace("/\{([\\\$a-zA-Z0-9_\[\]\\\ \-\'\,\&\;\%\*\/\.\(\)\>\'\"\$\x7f-\xff]+)\}/ies",  "\$this->__replace('\\1')", $string);		
		if($string1==$string)
		{
		 $string1 =  str_replace("'", "",$string1);	
		 $string1 = preg_replace("/\\\$([\\\$a-zA-Z0-9_\[\]\\\ \-\'\,\;\%\(\)\*\/\.\>\'\"\$\x7f-\xff]+)/ies", "\$this->var_add_str('\\1')", $string1);		 
         return '<?php echo $this->_write_url_html("'.$string1.'"); ?>';			
		}
		else
		{	
			
		 $string1 =  str_replace("'", "",$string1);		
		 $string1 = preg_replace("/\\\$([\\\$a-zA-Z0-9_\[\]\\\ \-\'\,\;\%\(\)\*\/\.\>\'\"\$\x7f-\xff]+)/ies", "\$this->var_add_str('\\1')", $string1);
         return '<?php echo $this->_write_url_html("'.$string1.'"); ?>';			
		
		}	
				
    }

     function _urlmodhtml($string)
	{	   
		$string1 = preg_replace("/\{([\\\$a-zA-Z0-9_\[\]\\\ \-\'\,\&\;\%\*\/\.\(\)\>\'\"\$\x7f-\xff]+)\}/ies",  "\$this->__replace('\\1')", $string);		
		if($string1==$string)
		{
		 $string1 =  str_replace("'", "",$string1);	
		 $string1 = preg_replace("/\\\$([\\\$a-zA-Z0-9_\[\]\\\ \-\'\,\;\%\(\)\*\/\.\>\'\"\$\x7f-\xff]+)/ies", "\$this->var_add_str('\\1')", $string1);		 
         return '<?php echo $this->_write_url_modhtml("'.$string1.'"); ?>';			
		}
		else
		{	
			
		 $string1 =  str_replace("'", "",$string1);		
		 $string1 = preg_replace("/\\\$([\\\$a-zA-Z0-9_\[\]\\\ \-\'\,\;\%\(\)\*\/\.\>\'\"\$\x7f-\xff]+)/ies", "\$this->var_add_str('\\1')", $string1);
         return '<?php echo $this->_write_url_modhtml("'.$string1.'"); ?>';			
		
		}	
				
    }

	function var_add_str($str='') 
	{

		if(empty($str))return '';
		/* $str = str_replace(array('[',']'),array("['","']"), $str);*/	
		 $str = str_replace(array('[',']'),array('["','"]'), $str);			 	
		return '".$'.$str.'."';
	}

	function _include($filename) {
		$vfilename = $this->template_dir . '/' . $filename;
		$out = file_get_contents($vfilename);
		$out = $this->template_str($out);
		return $out;
	}

	function __replace($string) {

		return str_replace('\"', '"', $string);
	}
	
	function quottostr($string) 
	{
		return str_replace(array('&quot;','&#39;'),array('"',"'"), $string);
	}
	function picdir($string) {
		$root_dir = (UPFILE_PATH == '') ? (SITE_URL . '/upfiles') : (SITE_URL . '/' . UPFILE_PATH);
		return $root_dir . '/' . $string;
	}

	function owndate($value) {
		return date("Y-m-d G:i:s", $value);
	}
    /*
	$powerstr 的值的写法是比如：r_2,r_3,u_12,app_scup_leaveword_DIAN_0_index_OR_job_index_AND_order_index_AND_aa_index_OR_pic_index,lc_doscore_relogic，其中r开头的表示用户组然后加id号，u开头的表示用户编号id号,lc开头的表示调用doscore逻辑的relogic函数
	,该函数表示，用户组2，3可以查看信息，用户id为12的可以查看信息，运行doscore的relogic返回值为1则可以查看信息，否则不能查看信息，信息部分以"*"替换
    app开头的表示该登入用户是否有scup-leaveword:0-index||job-index&&order-index&&aa-index||pic-index这个的权限的逻辑计算,书写方法':'用_DIAN_代替,'||'用'_OR_'代替,'&&'用'_AND_'代替,'-'用'_'代替
	$default 的值只能写26个字母和数字，如果$default的值是一个变量名的名称，则$default的值就是该变量名的值，比如我定义一个变量$varnamevalue = '错误'  然后
	让$default='varnamevalue',则最后$default='错误'了。
	*/
	function fieldshowpower($showvalue,$powerstr, $default = '******') 
	{
        if(isset($this->_var[$default]))
		{
		 $default = $this->_var[$default];
		}
	    $temp = $temp1 = $temp2 = $temp3 = array();	
		
	    $temp = explode(",",$powerstr);			
        $temp = del_empty_var($temp);
        foreach($temp as $key => $value)
	    {
		 $temp1 = explode("_",$value);
         $temp1 = del_empty_var($temp1);		      
	     $temp2[$key] = $temp1;
	    }

		foreach($temp2 as $key => $value)
		{	
		
		
		    if($value[0]=='r')
			{
			    if($GLOBALS['SCup']->SCup_user_msg['data']['role_id']=='admin:'.$value[1].'|scup:'.$value[1])
			    {
				  return $showvalue;				
				}
			}
		    if($value[0]=='u')
			{
			    if($GLOBALS['SCup']->SCup_user_msg['user_id']==$value[1])
			    {
				  return $showvalue;				
				}
			}
			
		    if($value[0]=='app')
			{
			    $appstr = '';
				foreach($value as $key1 => $value1)
				{
				   if($key1==0)
				   {
				    continue ;
				   }
				   if($value1=='DIAN')
				   {  
				      if(!isset($value[$key1+1]))
					  {
					    $value1 = ':-0';	
					  }else
					  {
				         $value1 = ':';					  
					  }

				   }else if($value1=='OR')
				   {
				      $value1 = '||';				   
				   }else if ($value1=='AND')
				   {
				      $value1 = '&&';				   
				   }
				   $appstr .= $value1.'-';
				}
				$appstr = substr($appstr, 0,-1);
                $appstr = str_replace(array('-:-','-||-','-&&-'),array(':','||','&&'),$appstr);		
                if(icon_power_limit($appstr))
				{
				   return $showvalue;
				}
			}
									
		    if($value[0]=='lc')
			{
			    $return_value = '';
	            $app = $value[1];
	            $act = $value[2];	
						
				if(!isset($this->_logic_app[$app.'_'.$act]))
				{				
 	               $logic_app_file = ROOT_PATH . '/data/logic/' . $app .'.logic.php';
		           require_once ($logic_app_file);			   		
		           $app_class_name = ucfirst($app) . 'App';
		           $logicapp = new $app_class_name ();		
		           $logicapp->do_action($act);
				   $this->_logic_app[$app.'_'.$act] = $logicapp->_act_return;
		           $logicapp->destruct();				
				}
				if($this->_logic_app[$app.'_'.$act])
				{ 
				  return $showvalue;				
				}	              
			}				
			
		}
	    return $default;		
	}

	function _filterworlds($value = '') {
		if( $this->filterworlds ==null && defined("FILTER_WORLD_FILEDIR") && FILTER_WORLD_FILEDIR && file_exists(ROOT_PATH . '/scup/view/'.VIEW_TYPE.'/icon/icontempdata/'.FILTER_WORLD_FILEDIR)){		    
			$dir = ROOT_PATH . '/scup/view/'.VIEW_TYPE.'/icon/icontempdata/'.FILTER_WORLD_FILEDIR;
			$data = @include($dir);
            foreach($data['data'] as $key1 => $value1)
	        {   
			    if(!empty($value1['datakey'])) {
				$this->filterworlds[$value1['datakey']] = $value1['datavalue'];
				}                
	        } 			
		}		
	    if($this->filterworlds !==null) {
            foreach($this->filterworlds as $key2 => $value2)
	        {   
                $value = str_replace($key2, $value2, $value); 			
	        } 		    
		}
        return $value; 
	}	
	
	function var_replace($string) {
		$s = explode('|', $string);

		$string = explode('.', $s[0]);

		if (preg_match('/^(_key|_value|_GET|_POST|_REQUEST|GLOBALS)?[0-9]*$/i', $string[0]))
		{
			$k = '$' . $string[0];
		} else 
		{
		
			   if(strstr($string[0], '$')||strstr($string[0], '#'))
			   {
              $k = '$this->_var[' . $string[0] . ']';
			   }
			   else
			   {
              $k = '$this->_var[\'' . $string[0] . '\']';
			   }		
		
			
		}

		for ($i = 1; $i < count($string); $i++) {
			if (!empty ($string[$i])||$string[$i]=='0') 
			{
			   if(strstr($string[$i], '$')||strstr($string[$i], '#'))
			   {
			   $k .= '[' . $string[$i] . ']';
			   }
			   else
			   {
			   $k .= '[\'' . $string[$i] . '\']';
			   }
			}
		}
        if(!isset($s[1]))
		{
		$s[1] ='';
		}
		$sk = explode(':', $s[1]);
		$tt = array();
		foreach($sk as $key => $value)
		{  
		 if(strstr($value, '$'))
		 {  
		    $str_tep = explode('.', $value);
			$str_tep[0] = str_replace(array('$'),array(''),$str_tep[0]);
		    if (preg_match('/^(_key|_value|_GET|_POST|_REQUEST|GLOBALS)?[0-9]*$/i', $str_tep[0]))
		    {
			$tt[$key] = '$' . $str_tep[0];
		    } else 
		    {
		
			   if(strstr($str_tep[0], '$')||strstr($str_tep[0], '#'))
			   {
              $tt[$key] = '$this->_var[' . $str_tep[0] . ']';
			   }
			   else
			   {
              $tt[$key] = '$this->_var[\'' . $str_tep[0] . '\']';
			   }		
		    }

		    for ($i = 1; $i < count($str_tep); $i++) {
			   if (!empty ($str_tep[$i])||$str_tep[$i]=='0') 
			   {
			      if(strstr($str_tep[$i], '$')||strstr($str_tep[$i], '#'))
			      {
			      $tt[$key] .= '[' . $str_tep[$i] . ']';
			      }
			      else
			      {
			       $tt[$key] .= '[\'' .$str_tep[$i] . '\']';
			      }
			   }
		    }			
		
		 }
		}		
		
		$s[1] = trim($sk[0]);
		switch ($s[1]) {
			case 'escape' :
				$s[1] = trim($sk[1], '"');
				if ($s[1] == 'html') {
					$k = 'htmlspecialchars(' . $k . ')';
				}
				else if ($s[1] == 'url') {
					$k = 'urlencode(' . $k . ')';
				}
				else if ($s[1] == 'quotes') {
					$k = 'addslashes(' . $k . ')';
				}
				else if ($s[1] == 'input') {
					$k = 'str_replace(\'"\',\'&quot;\',' . $k . ')'; //str_replace当中不能用空格
				}
				else if ($s[1] == 'editor') {
					$k = 'html_filter(' . $k . ')';
				} else {
					$k = 'htmlspecialchars(' . $k . ')';
				}

				break;

			case 'nl2br' :
				$k = 'nl2br(' . $k . ')';
				break;

			case 'dateymd' :
				$k = 'date("Y-m-d",' . $k . ')';
				break;

			case 'dateymdgis' :
				$k = '$this->owndate(' . $k . ')';
				break;
			case 'picdir' :
				$k = '$this->picdir(' . $k . ')';
				break;
			case 'quottostr' :
				$k = '$this->quottostr(' . $k . ')';
				break;
			case 'filterworld' :
				$k = '$this->_filterworlds(' . $k . ')';
				break;				
			case 'cutstr' :
			    if(empty($sk[1])||$sk[1]<=0)$sk[1]=20;
				$k = 'psp_cutstr(' . $k . ','.$sk[1].')';
				break;
			case 'filtrate' :
				$k = 'filtrate(' . $k . ')';
				break;	
			case 'fieldshowpower' :
			    if(!isset($sk[2]))$sk[2]='******';
				if(isset($tt[1]))
				{
				$k = '$this->fieldshowpower(' . $k . ','.$tt[1].',"'.$sk[2].'")';				
				}else
				{
				$k = '$this->fieldshowpower(' . $k . ',"'.$sk[1].'","'.$sk[2].'")';				
				}
				break;				
							
			default :
				break;
		}
		$k =  str_replace("#", "$",$k);
		return $k;
	}
}
?>